<?php
namespace app\sso\controller;

use think\facade\Env;
use app\BaseController;
use app\common\util\ReturnCode;
use think\Response;

class Base extends BaseController
{
    public $debug = false;
    /**
     * 成功的返回
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return Response
     */
    public function buildSuccess(array $data = [], string $msg = '操作成功', int $code = ReturnCode::SUCCESS): Response {
        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (Env::get('APP_DEBUG') && $this->debug) {
            $return['debug'] = $this->debug;
        }

        return json($return);
    }
    /**
     * 错误的返回
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return Response
     */
    public function buildFailed(int $code, string $msg = '操作失败', array $data = []): Response {
        $return = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        if (Env::get('APP_DEBUG') && $this->debug) {
            $return['debug'] = $this->debug;
        }

        return json($return);
    }
}